﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using KPIS.DBM;
using KPIS.GERP.GBMS.MODEL;
namespace KPIS.GERP.GBMS.DAL
{
    public class UserDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
        public DataSet Login(string Code, string Password)
        {
            string strSQL;
            DataSet ds;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@name", Code);
                dbManager.AddParameters(1, "@password", Password);

                strSQL = "SELECT *"
                         + ", cast(concat(user_first_name, ' ', user_last_name) as char) as user_name"
                         + " FROM SYS_SC_USERS"
                         + " WHERE USER_CODE = @name"
                            + " AND USER_PASSWORD = @password";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return ds;
        }
        public DataSet LoginByUserSeq(string UserSeq)
        {
            string strSQL;
            DataSet ds;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@user_seq", UserSeq); 

                strSQL = "SELECT *"
                         + ", cast(concat(user_first_name, ' ', user_last_name) as char) as user_name"
                         + " FROM SYS_SC_USERS"
                         + " WHERE USER_SEQ = @user_seq";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return ds;
        }
        public string GetUserFullNameByEmpSeq(int empSeq)
        {
            string result = "";
            string strSQL;
            DataTable dt;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@emp_seq", empSeq);

                strSQL = @"SELECT first_name, last_name, n.prefix_name
                            FROM hrm_ps_employees e 
                            LEFT JOIN mas_name_prefixes n ON e.name_prefix_seq = n.name_prefix_seq
                            WHERE emp_seq = @emp_seq";

                dt = dbManager.ExecuteDataTable(CommandType.Text, strSQL);
                if(dt.Rows.Count > 0)
                {
                    result = Convert.ToString(dt.Rows[0]["prefix_name"]) + " " +
                             Convert.ToString(dt.Rows[0]["first_name"]) + "  " +
                             Convert.ToString(dt.Rows[0]["last_name"]);
                    result = result.Trim();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return result;
        }
    }
}
